# -*- coding: utf-8 -*-

# 益智问答游戏

import sys, pygame
from pygame.locals import *

reload(sys)
sys.setdefaultencoding('utf-8')

white = 255,255,255
purple = 255,0,255
yellow = 255,255,0
green = 0,255,0
red = 255,0,0

pygame.init()
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("益智问答游戏")
font1 = pygame.font.Font("mini.ttf", 30)
font2 = pygame.font.Font("mini.ttf", 16)

class Trivia(object):
    def __init__(self, filename):
        self.data = []
        self.current = 0
        self.total = 0
        self.correct = 0
        self.score = 0
        self.scored = False
        self.failed = False
        self.wronganswer = 0
        self.colors = [white,white,white,white]

        f = open(filename, 'r')
        trivia_data = f.readlines()
        f.close()

        for text_line in trivia_data:
            self.data.append(text_line.strip())
            self.total += 1

    def show_question(self):
        self.print_text(font1, 210, 5, u"益智问答游戏")
        self.print_text(font2, 230, 500-20, u"按(1-4)键回答问题", purple)
        self.print_text(font2, 530, 5, u"SCORE", purple)
        self.print_text(font2, 550, 25, str(self.score), purple)

        self.correct = int(self.data[self.current+5])

        question = self.current
        self.print_text(font1, 5, 80, u"问题 "+str(int(question/6)+1))
        self.print_text(font2, 20, 120, self.data[self.current].decode('utf-8'), yellow)

        if self.scored:
            self.colors = [white, white, white, white]
            self.colors[self.correct-1] = green
            self.print_text(font1, 270, 380, u"正确 ", green)
            self.print_text(font2, 230, 420, u"按回车键到下一题", green)
        elif self.failed:
            self.colors = [white, white, white, white]
            self.colors[self.wronganswer-1] = red
            self.colors[self.correct - 1] = green
            self.print_text(font1, 270, 380, u"错误 ", red)
            self.print_text(font2, 230, 420, u"按回车键到下一题", red)

        # 显示答案
        self.print_text(font1, 5, 170, u"答案")
        self.print_text(font2, 20, 210, u"1 - " + self.data[self.current+1].decode('utf-8'), self.colors[0])
        self.print_text(font2, 20, 240, u"2 - " + self.data[self.current+2].decode('utf-8'), self.colors[1])
        self.print_text(font2, 20, 270, u"3 - " + self.data[self.current+3].decode('utf-8'), self.colors[2])
        self.print_text(font2, 20, 300, u"4 - " + self.data[self.current+4].decode('utf-8'), self.colors[3])

    def print_text(self, font, x, y, text, color=white, shadow=True):
        if shadow:
            imgText = font.render(text, True, (0,0,0))
            screen.blit(imgText, (x-2,y-2))
        image = font.render(text, True, color)
        screen.blit(image, (x, y))

    def handle_input(self, number):
        if not self.scored and not self.failed:
            if number == self.correct:
                self.scored = True
                self.score += 1
            else:
                self.failed = True
                self.wronganswer = number

    def next_question(self):
        if self.scored or self.failed:
            self.scored = False
            self.failed = False
            self.correct = 0
            self.colors = [white, white, white, white]
            self.current += 6
            if self.current >= self.total:
                self.current = 0

trivia = Trivia('trivia_data.txt')

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
        elif event.type == KEYUP:
            if event.key == pygame.K_ESCAPE:
                sys.exit()
            elif event.key == pygame.K_1:
                trivia.handle_input(1)
            elif event.key == pygame.K_2:
                trivia.handle_input(2)
            elif event.key == pygame.K_3:
                trivia.handle_input(3)
            elif event.key == pygame.K_4:
                trivia.handle_input(4)
            elif event.key == pygame.K_RETURN:
                trivia.next_question()

    screen.fill((0, 0, 200))
    trivia.show_question()
    pygame.display.update()


